package com.turing.post.cardmanagement.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.cardmanagement.util.CardClassicBindDistrict;
import com.turing.post.cardmanagement.util.CardClassicBindDistrictDao;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;

/**
 * 卡种类绑定商圈业务实现
 * @author zxppanda
 *
 */
@Stateless(mappedName = "cardmanagement.impl.CardClassicBindDistrictDaoImpl")
@Remote
@Local
public class CardClassicBindDistrictDaoImpl implements
		CardClassicBindDistrictDao {

	@PersistenceContext
	protected EntityManager _em;

	@Override
	public CardClassicBindDistrict getCardClassicBindDistrictByPk(
			CardClassicBindDistrict ccbd) {
		return _em.find(CardClassicBindDistrict.class, ccbd);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CardClassicBindDistrict> getCardClassicBindDistrictListByCardClassicId(
			BigInteger cardClassicId) {
		String sql = "select * from CardClassicBindDistrict where CardClassicId=?1 order by businessDistrictId";
		Query query = _em.createNativeQuery(sql, CardClassicBindDistrict.class);
		query.setParameter(1, cardClassicId);
		return query.getResultList();
	}

	@Override
	public void deleteCardClassicBindDistrict(BigInteger cardClassicId) {
		String sql = "delete from CardClassicBindDistrict where CardClassicId =?1";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, cardClassicId);
		query.executeUpdate();

	}

	@Override
	public void insertCardClassicBindDistrict(BigInteger cardClassicId,
			BigInteger[] arrInt) throws PCPSApplicationException {
		// 删除已绑定数据
		deleteCardClassicBindDistrict(cardClassicId);
		CardClassicBindDistrict ccbd = null;
		for (BigInteger b : arrInt) {
			ccbd = new CardClassicBindDistrict();
			ccbd.setCardClassicId(cardClassicId);
			ccbd.setBusinessDistrictId(b);
			insertCardClassicBindDistrict(ccbd);
		}
	}

	@Override
	public void insertCardClassicBindDistrict(CardClassicBindDistrict ccbd)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, ccbd);
	}
}